<template>
  <div class="address">
    <van-nav-bar
      title="地址管理"
      left-text="返回"
      left-arrow
      @click-left="back"
    />
    <van-address-list
      :list="list"
      default-tag-text="默认"
      :switchable="false"
      @add="onAdd"
      @edit="onEdit"
    />
  </div>
</template>

<script>
import "../assets/less/address.less";
export default {
  name: "Address",
  data() {
    return {
      list: [],
    };
  },
  created() {
    this.getAddressData();
  },
  methods: {
    back() {
      this.$router.go(-1);
    },
    onAdd() {
      this.$router.push({ name: "NewAddress" });
    },
    onEdit({ aid }) {
      this.$router.push({ name: "NewAddress", params: { aid } });
    },
    getAddressData() {
      let tokenString = this.$cookies.get("tokenString");
      if (!tokenString) {
        return this.$router.push({ name: "Login" });
      }
      this.$toast.loading({
        message: "加载中",
        forbidClick: true,
        duration: 0,
      });
      this.$axios({
        method: "GET",
        url: "/findAddress",
        params: {
          appkey: this.appkey,
          tokenString,
        },
      })
        .then((res) => {
          // console.log("res getAddressData ==> ", res);
          this.$toast.clear();

          if (res.data.code == 700) {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 1000,
            });
            return this.$router.push({ name: "Login" });
          }

          if (res.data.code === 20000) {
            res.data.result.forEach((ele) => {
              ele.isDefault = Boolean(ele.isDefault);
              ele.address =
                ele.province + ele.city + ele.county + ele.addressDetail;
            });
            this.list = res.data.result;
            // console.log(this.list)
          } else {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 1000,
            });
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log("err ==> ", err);
        });
    },
  },
};
</script>

<style lang="less" scoped>
</style>